package cn.edu.csust.cvs.management.controller;

import cn.edu.csust.cvs.management.common.ApiPageResult;
import cn.edu.csust.cvs.management.common.ApiResponse;
import cn.edu.csust.cvs.management.common.ApiResult;
import cn.edu.csust.cvs.management.common.PageQueryRequest;
import cn.edu.csust.cvs.management.controller.request.*;
import cn.edu.csust.cvs.management.repository.entity.VideoEntity;
import cn.edu.csust.cvs.management.service.VideoService;
import cn.edu.csust.cvs.management.service.domain.Video;
import cn.edu.csust.cvs.management.service.domain.VideoClip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;


/**
 * @Auther: snail
 * @Date: 2021/1/22 00:30
 * @Description: 视频管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/cvsm/video")
@Api(tags = "视频管理")
public class VideoController {

    @Autowired
    VideoService videoService;

    @ApiOperation(value = "上传视频文件", notes = "上传视频文件")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResponse upload(@RequestParam(value = "multipartFile") MultipartFile multipartFile) {

        return videoService.upload(multipartFile);
    }

    @ApiOperation(value = "分页查询视频列表", notes = "分页查询视频列表")
    @GetMapping("/pagination")
    public ApiPageResult<List<VideoEntity>> pageQuery(@Valid VideoPageQueryRequest request, BindingResult result) {

        return videoService.pageQuery(request);
    }

    @ApiOperation(value = "查询视频详情", notes = "查询视频详情")
    @GetMapping("/{id}")
    public ApiResult<Video> detail(@PathVariable("id")Integer id) {

        return videoService.detail(id);
    }

    @ApiOperation(value = "视频删除", notes = "视频删除")
    @DeleteMapping("")
    public ApiResponse delete(@Valid @RequestBody VideoDeleteRequest request, BindingResult result) {

        return videoService.delete(request);
    }

    @ApiOperation(value = "固定时长切分", notes = "固定时长切分")
    @PostMapping("/fix/segmentation")
    public ApiResponse segment(@Valid @RequestBody VideoFixSegmentRequest request, BindingResult result) {

        return videoService.segmentByFix(request);
    }

    @ApiOperation(value = "自由时长切分", notes = "自由时长切分")
    @PostMapping("/flexible/segmentation/{id}")
    public ApiResponse segment(@PathVariable("id") Integer id, @Valid @RequestBody VideoFlexibleSegmentRequest request, BindingResult result) {

        return videoService.segmentByFlexible(id,request);
    }

    @ApiOperation(value = "语音提取", notes = "语音提取")
    @PostMapping("/speech/extract")
    public ApiResponse extract(@Valid @RequestBody SpeechExtractRequest request, BindingResult result) {

        return videoService.extract(request);
    }

    @ApiOperation(value = "语音转文本", notes = "语音转文本")
    @PostMapping("/speech/to/text/{speechId}")
    public ApiResult<String> stt(@PathVariable("speechId") Integer speechId) {

        return videoService.stt(speechId);
    }

    @ApiOperation(value = "文本转语音", notes = "文本转语音")
    @PostMapping("/text/to/speech/{speechId}")
    public ApiResponse tts(@PathVariable("speechId") Integer speechId,String text) {
        log.info("Enter method text to speech speechId = {},text = {}",speechId,text);
        return videoService.tts(speechId,text);
    }

    @ApiOperation(value = "语音视频合成", notes = "语音视频合成")
    @PostMapping("/speech/composite")
    public ApiResponse composite(@Valid @RequestBody SpeechCompositeRequest request, BindingResult result) {

        return videoService.composite(request);
    }

    @ApiOperation(value = "录音视频合成", notes = "录音视频合成")
    @PostMapping("/record/composite")
    public ApiResponse composite(@Valid @RequestBody RecordCompositeRequest request, BindingResult result) {

        return videoService.composite(request);
    }

    @ApiOperation(value = "视频片段合并", notes = "视频片段合并")
    @PostMapping("/clip/merge/{id}")
    public ApiResponse merge(@PathVariable("id")Integer id) {

        return videoService.merge(id);
    }

    @ApiOperation(value = "分页查询视频片段", notes = "分页查询视频片段")
    @GetMapping("/clip/pagination/{videoId}")
    public ApiPageResult<List<VideoClip>> pageQuery(@PathVariable("videoId") Integer videoId, @Valid PageQueryRequest request) {

        return videoService.pageQuery(videoId,request);
    }
}
